﻿#region Vương văn hùng
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using DataAccess;
using Entities;
using Common;

namespace BusinessLogic
{
    public static class ChiTietHangHoaNhaCungCap
    {
        public static Entities.ChiTietHangHoaNhaCungCap[] SelectChiTietHangHoaNhaCungCap(Entities.ChiTietHangHoaNhaCungCap chitiet)
        {
            Entities.ChiTietHangHoaNhaCungCap[] list = null;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.ChiTietHangHoaNhaCungCap.sp_Select_ChiTietHangHoaNhaCungCap, cn);
                cmd.Parameters.Add(Common.Constants.ChiTietHangHoaNhaCungCap.HanhDong, SqlDbType.NVarChar).Value = chitiet.HanhDong;
                cmd.Parameters.Add(Common.Constants.ChiTietHangHoaNhaCungCap.MaNhaCungCap, SqlDbType.VarChar).Value = chitiet.MaNhaCungCap;
                cmd.Parameters.Add(Common.Constants.ChiTietHangHoaNhaCungCap.MaHangHoa, SqlDbType.VarChar).Value = chitiet.MaHangHoa;
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.ChiTietHangHoaNhaCungCap row = new Entities.ChiTietHangHoaNhaCungCap();
                    row.MaNhaCungCap = dr[Common.Constants.ChiTietHangHoaNhaCungCap.MaNhaCungCap].ToString();
                    row.TenNhaCungCap = dr[Common.Constants.ChiTietHangHoaNhaCungCap.TenNhaCungCap].ToString();
                    row.MaHangHoa = dr[Common.Constants.ChiTietHangHoaNhaCungCap.MaHangHoa].ToString();
                    row.TenHangHoa = dr[Common.Constants.ChiTietHangHoaNhaCungCap.TenHangHoa].ToString();
                    row.MaDonViTinh = dr[Common.Constants.ChiTietHangHoaNhaCungCap.MaDonViTinh].ToString();
                    row.TenDonViTinh = dr[Common.Constants.ChiTietHangHoaNhaCungCap.TenDonViTinh].ToString();
                    row.GhiChu = dr[Common.Constants.ChiTietHangHoaNhaCungCap.GhiChu].ToString();
                    row.Deleted = Boolean.Parse(dr[Common.Constants.ChiTietHangHoaNhaCungCap.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0)
                {
                    list = null;
                }
                else
                {
                    list = new Entities.ChiTietHangHoaNhaCungCap[n];
                    for (int i = 0; i < n; i++)
                    {
                        list[i] = (Entities.ChiTietHangHoaNhaCungCap)arr[i];
                    }
                }
                //xoa bo nho dong ket noi
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                list = null;
            }
            return list;
        }
        public static int InsertUpdateList(Entities.ChiTietHangHoaNhaCungCap[] cungcap)
        {
            int rt = 0;
            try
            {
                if (cungcap != null)
                {
                    try
                    {
                        Entities.ChiTietHangHoaNhaCungCap deleted = new Entities.ChiTietHangHoaNhaCungCap();
                        deleted.HanhDong = "0";
                        deleted.MaNhaCungCap = cungcap[0].MaNhaCungCap;
                        Delete(deleted);
                    }
                    catch (Exception)
                    {
                    }
                    for (int i = 0; i < cungcap.Length; i++)
                    {
                        rt = InsertUpdate(cungcap[i]);
                        if (rt == 0)
                        {
                            break;
                        }
                        else
                        { continue; }
                    }

                }
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                rt = 0;
            }
            return rt;
        }
        public static int InsertUpdate(Entities.ChiTietHangHoaNhaCungCap cungcap)
        {
            int rt = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.ChiTietHangHoaNhaCungCap.sp_InsertUpdate_ChiTietHangHoaNhaCungCap, cn);
                cmd.Parameters.Add(Common.Constants.ChiTietHangHoaNhaCungCap.HanhDong, SqlDbType.NVarChar).Value = cungcap.HanhDong;
                cmd.Parameters.Add(Common.Constants.ChiTietHangHoaNhaCungCap.MaNhaCungCap, SqlDbType.VarChar).Value = cungcap.MaNhaCungCap;
                cmd.Parameters.Add(Common.Constants.ChiTietHangHoaNhaCungCap.TenNhaCungCap, SqlDbType.NVarChar).Value = cungcap.TenNhaCungCap;
                cmd.Parameters.Add(Common.Constants.ChiTietHangHoaNhaCungCap.MaHangHoa, SqlDbType.VarChar).Value = cungcap.MaHangHoa;
                cmd.Parameters.Add(Common.Constants.ChiTietHangHoaNhaCungCap.TenHangHoa, SqlDbType.NVarChar).Value = cungcap.TenHangHoa;
                cmd.Parameters.Add(Common.Constants.ChiTietHangHoaNhaCungCap.MaDonViTinh, SqlDbType.VarChar).Value = cungcap.MaDonViTinh;
                cmd.Parameters.Add(Common.Constants.ChiTietHangHoaNhaCungCap.TenDonViTinh, SqlDbType.NVarChar).Value = cungcap.TenDonViTinh;
                cmd.Parameters.Add(Common.Constants.ChiTietHangHoaNhaCungCap.GhiChu, SqlDbType.NVarChar).Value = cungcap.GhiChu;
                cmd.Parameters.Add(Common.Constants.ChiTietHangHoaNhaCungCap.Deleted, SqlDbType.Bit).Value = cungcap.Deleted;
                rt = cmd.ExecuteNonQuery();
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                rt = 0;
            }
            return rt;
        }

        public static int Delete(Entities.ChiTietHangHoaNhaCungCap cungcap)
        {
            int rt = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.ChiTietHangHoaNhaCungCap.sp_Deleted_ChiTietHangHoaNhaCungCap, cn);
                cmd.Parameters.Add(Common.Constants.ChiTietHangHoaNhaCungCap.HanhDong, SqlDbType.NVarChar).Value = cungcap.HanhDong;
                cmd.Parameters.Add(Common.Constants.ChiTietHangHoaNhaCungCap.MaNhaCungCap, SqlDbType.VarChar).Value = cungcap.MaNhaCungCap;
                rt = cmd.ExecuteNonQuery();
                //xoa bo nho dong ket noi
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                rt = 0;
            }
            return rt;
        }
    }
}
#endregion
